let form = layui.form;
// 裁剪选框
let image = $('#image')
var options = {
  aspectRatio: 400 / 280,
  preview: '.img-preview'
}
image.cropper(options);
// 富文本编辑器
initEditor()
// 下拉列表
axios('/my/category/list').then((res) => {
  // console.log(res);
  let data = res.data.data;
  let str = ''
  data.forEach(item => {
    str += `<option value="${item.id}">${item.name}</option>`
  });
  // console.log(str);
  $('select[name=cate_id]').append(str);
  form.render('select');
})
// 选择封面，触发文件域的点击事件
$('button:contains("选择封面")').on('click', function () {
  $('input[type=file]').trigger('click')
})
// 改变图片
$('input[type=file]').on('change', function () {
  if (this.files.length > 0) {
    let file = this.files[0];
    let url = URL.createObjectURL(file)
    image.cropper('replace', url)
  }
})
// 提交数据

$('form').on('submit', function (e) {
  e.preventDefault();
  let data = new FormData(this)
  data.forEach((item, index) => console.log(index, item))
  let canvas = image.cropper('getCroppedCanvas', {
    width: 400,
    height: 280
  })
  data.set('content', tinyMCE.activeEditor.getContent());
  canvas.toBlob((blob) => {
    data.append('cover_img', blob)
    axios.post('/my/article/add', data).then((res) => {
      let { status, message } = res.data
      if (status === 0) {
        layer.msg(message)
        location.href = './articleList.html'
      }
    })
  })


})